package com.hongxun.design.adapters;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.alipay.AlipayUser;
import com.hongxun.design.bean.SettingItemBean;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.IntentToActivity;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.weixinpay.WxpayUser;

import java.util.List;

/**
 * Created by zerdoor_pc on 2016/8/8.
 */
public class DialogPayAdapter extends BaseAdapter {
    Context context;
    List<SettingItemBean> itemBeans;
    LayoutInflater inflater;
    AlertDialog dialog;
    String buy_menu_id, people_num, type;

    public DialogPayAdapter(Context context, List<SettingItemBean> itemBeans, String buy_menu_id, String people_num, String type,AlertDialog dialog) {
        this.context = context;
        this.itemBeans = itemBeans;
        this.buy_menu_id = buy_menu_id;
        this.people_num = people_num;
        this.dialog = dialog;
        this.type = type;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return itemBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return itemBeans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        SettingItemBean bean = (SettingItemBean) getItem(i);
        Holldler holldler = null;
        if (view == null) {
            view = inflater.inflate(R.layout.listview_item_pay, null);
            holldler = new Holldler(view);
            view.setTag(holldler);
        } else {
            holldler = (Holldler) view.getTag();
        }

        try {
            holldler.title.setText(bean.getTitle());
            SetTypeface.set(holldler.icon, bean.getValue());
            holldler.icon.setTextColor(context.getResources().getColor(bean.getType()));
            holldler.item.setOnClickListener(new MClickListener(i, bean) {
                @Override
                public void onClick(View view, int position, Object msg) {
                    SettingItemBean bean = (SettingItemBean) msg;
                    String payid = bean.getCode();
                    Bundle bundle=new Bundle();
                    bundle.putString("payid",payid);
                    bundle.putString("buy_menu_id",buy_menu_id);
                    bundle.putString("people_num",people_num);
                    bundle.putString("type", type);
                    if (payid.equals(MString.wx)){
                        //ToastShow.showShort(context,"微信支付！");
                        IntentToActivity.intent((Activity) context, WxpayUser.class, bundle);
                    }else  if (payid.equals(MString.ali)){
                        IntentToActivity.intent((Activity) context, AlipayUser.class, bundle);

                    }else{
                        ToastShow.showShort(context,"支付信息异常,请重试！");
                    }
                    dialog.dismiss();
                    MyLog.L(MString.SUCCESSED + "11== payid=" + payid + "&buy_menu_id=" + buy_menu_id + "&people_num=" + people_num + "&type=" + type);
                            // MyLog.L(MString.SUCCESSED + " http://192.168.1.61:7011/payControl/payment?payid=" + payid + "&buy_menu_id=" + buy_menu_id + "&people_num=" + people_num + "&type=" + type + "&openIdent=" + MyApp.openIdent);
                }
            });
        } catch (Exception e) {
        }
        return view;
    }

    class Holldler {
        RelativeLayout item;
        TextView icon, title, right;
        Holldler(View v) {
            item = (RelativeLayout) v.findViewById(R.id.pay_item);
            icon = (TextView) v.findViewById(R.id.pay_icon);
            title = (TextView) v.findViewById(R.id.pay_title);
            right = (TextView) v.findViewById(R.id.pay_right);
            SetTypeface.set(right, IconSourse.RIGHTARROW);
        }
    }
}
